package com.practice.springboot_01_zwj.controller;

import com.github.pagehelper.PageInfo;
import com.practice.springboot_01_zwj.entity.ProjectReport;
import com.practice.springboot_01_zwj.entity.ReportDetails;
import com.practice.springboot_01_zwj.entity.vo.R;
import com.practice.springboot_01_zwj.mapper.ProjectReportMapper;
import com.practice.springboot_01_zwj.mapper.ReportDetailsMapper;
import com.practice.springboot_01_zwj.service.ProjectReportService;
import com.practice.springboot_01_zwj.service.ReportDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/reportDetails")
@Validated
public class ReportDetailsController {

    @Autowired
    private ReportDetailsService reportDetailsService;
    
    /**
     * 根据keyId查询一级菜单和对应的二级菜单列表
     * 返回格式：projectReport对象包含reportDetailsList属性
     */
    @GetMapping("/selectProjectReportDetails/{keyId}")
    public R<Map<String, Object>> selectProjectReportDetails(@PathVariable Integer keyId) {
        try {
            // 查询一级菜单
            ProjectReport projectReport = reportDetailsService.getProjectReportByKeyId(keyId);
            if (projectReport == null) {
                return R.error(404, "未找到对应的项目");
            }

            // 查询二级菜单列表
            List<ReportDetails> reportDetailsList = reportDetailsService.getReportDetailsByKeyId(keyId);

            // 将二级菜单列表设置到一级菜单对象中
            projectReport.setReportDetailsList(reportDetailsList);

            // 构建返回数据
            Map<String, Object> data = new HashMap<>();
            data.put("projectReport", projectReport);

            return R.success("查询成功", data);
        } catch (Exception e) {
            return R.error("系统错误: " + e.getMessage());
        }
    }

    /**
     * 根据rid查询单个二级菜单
     */
    @GetMapping("/selectReportDetail/{rid}")
    public R<ReportDetails> selectReportDetail(@PathVariable Integer rid) {
        try {
            ReportDetails reportDetails = reportDetailsService.getReportDetailById(rid);
            if (reportDetails == null) {
                return R.error(404, "未找到对应的二级菜单");
            }

            return R.success("查询成功", reportDetails);
        } catch (Exception e) {
            return R.error("系统错误: " + e.getMessage());
        }
    }

    /**
     * 新增二级菜单
     */
    @PostMapping("/addReportDetail")
    public R<String> addReportDetail(@RequestBody ReportDetails reportDetails) {
        try {
            if (reportDetails.getKeyId() == null) {
                return R.error(400, "新增失败，keyId不能为空");
            }

            // 验证keyId是否存在
            ProjectReport projectReport = reportDetailsService.getProjectReportByKeyId(reportDetails.getKeyId());
            if (projectReport == null) {
                return R.error(400, "keyId不存在");
            }

            int result = reportDetailsService.addReportDetail(reportDetails);
            if (result > 0) {
                return R.success("新增成功", null);
            } else {
                return R.error("新增失败");
            }
        } catch (IllegalArgumentException e) {
            // 捕获时间验证的异常
            return R.error(400, e.getMessage());
        } catch (Exception e) {
            return R.error("系统错误: " + e.getMessage());
        }
    }

    /**
     * 更新二级菜单
     */
    @PutMapping("/updateReportDetail")
    public R<String> updateReportDetail(@RequestBody ReportDetails reportDetails) {
        try {
            if (reportDetails.getRid() == null) {
                return R.error(400, "更新失败，二级菜单ID不能为空");
            }

            // 检查记录是否存在
            ReportDetails existing = reportDetailsService.getReportDetailById(reportDetails.getRid());
            if (existing == null) {
                return R.error(404, "要更新的记录不存在");
            }

            // 验证keyId是否存在（如果传入了keyId）
            if (reportDetails.getKeyId() != null) {
                ProjectReport projectReport = reportDetailsService.getProjectReportByKeyId(reportDetails.getKeyId());
                if (projectReport == null) {
                    return R.error(400, "keyId不存在");
                }
            }

            int result = reportDetailsService.updateReportDetail(reportDetails);
            if (result > 0) {
                return R.success("更新成功", null);
            } else {
                return R.error("更新失败");
            }
        } catch (IllegalArgumentException e) {
            // 捕获时间验证的异常
            return R.error(400, e.getMessage());
        } catch (Exception e) {
            return R.error("系统错误: " + e.getMessage());
        }
    }

    /**
     * 删除二级菜单
     */
    @DeleteMapping("/deleteReportDetail/{rid}")
    public R<String> deleteReportDetail(@PathVariable Integer rid) {
        try {
            // 检查记录是否存在
            ReportDetails existing = reportDetailsService.getReportDetailById(rid);
            if (existing == null) {
                return R.error(404, "要删除的记录不存在");
            }

            int result = reportDetailsService.deleteReportDetail(rid);
            if (result > 0) {
                return R.success("删除成功", null);
            } else {
                return R.error("删除失败");
            }
        } catch (Exception e) {
            return R.error("系统错误: " + e.getMessage());
        }
    }

    /**
     * 根据keyId删除所有二级菜单
     */
    @DeleteMapping("/deleteReportDetailsByKeyId/{keyId}")
    public R<String> deleteReportDetailsByKeyId(@PathVariable Integer keyId) {
        try {
            int result = reportDetailsService.deleteReportDetailsByKeyId(keyId);
            return R.success("删除成功", null);
        } catch (Exception e) {
            return R.error("系统错误: " + e.getMessage());
        }
    }
}
